package manage

import (
	"github.com/gin-gonic/gin"
	"iot-base/auth/admin/logic"
	Log "iot-base/auth/logger"
	"iot-base/auth/svc"
	"iot-base/auth/types"
)

func StationModel(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	//uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	list, err := svc.Dao.TTModel.GetStationModel()
	if err != nil {
		Log.Log.Errorf("GetCataLogs [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{"list": list})
	return
}

func GetAreas(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	//uid, _ := c.Get("uid")
	resp := types.NewResult(c)
	var req types.ReqGetAreas
	err := c.ShouldBind(&req)
	if err != nil {
		Log.Log.Errorf("GetAreas [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if req.ParentId == "" {
		req.ParentId = "0"
	}
	list, err := svc.Dao.TAreas.QueryAreasByParentId(req.ParentId)
	if err != nil {
		Log.Log.Errorf("GetAreas [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{"list": list})
	return
}

func AddStation(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var req types.ReqAddStation
	err := c.ShouldBind(&req)
	if err != nil {
		Log.Log.Errorf("AddStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	err = logic.NewStationLogic().AddStation(req)
	if err != nil {
		Log.Log.Errorf("AddStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{})
	return
}

func GetStation(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var req types.ReqMgGetStation
	err := c.ShouldBind(&req)
	if err != nil {
		Log.Log.Errorf("AddStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	//_, err = logic.NewStationLogic().GetStation()
	var RespData types.RespMgGetStation
	err = svc.Dao.GetDb().Table("stations").Joins("INNER JOIN things on stations.uid =  things.uid and things.is_station = true").
		Select("stations.uid as station_id,stations.s_name,stations.area_id,stations.desc,stations.level,stations.lon,stations.lat,things.status_used,things.model_id ").
		Where("stations.uid = ?", req.StationId).Find(&RespData).Error
	if err != nil {
		Log.Log.Errorf("AddStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(RespData)
	return
}

func ModifyStation(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var req types.ReqMgModifyStation
	err := c.ShouldBind(&req)
	if err != nil {
		Log.Log.Errorf("ModifyStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	err = logic.NewStationLogic().ModifyStation(req)
	if err != nil {
		Log.Log.Errorf("ModifyStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.NetworkAnomaly, "")
		return
	}
	resp.Success(gin.H{})
	return
}

func DelStation(c *gin.Context) {
	requestId, _ := c.Get("RequestId")
	resp := types.NewResult(c)
	var req types.ReqMgDelStation
	err := c.ShouldBind(&req)
	if err != nil {
		Log.Log.Errorf("DelStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	// 站点下面是否存在设备
	thingNum, err := svc.Dao.TThingStation.CountStationThings(req.StationId)
	if err != nil {
		Log.Log.Errorf("DelStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if thingNum > 0 {
		resp.ErrorCode(types.Fail, "场站还有子设备没有删除")
		return
	}
	// 检查站点下面是否包含站点
	stationNum, err := svc.Dao.TStations.CountStationChild(req.StationId)
	if err != nil {
		Log.Log.Errorf("DelStation [%s] err:=%v", requestId, err)
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	if stationNum > 0 {
		resp.ErrorCode(types.Fail, "场站还有子场站没有删除")
		return
	}
	_ = svc.Dao.TStations.Delete(req.StationId)
	_ = svc.Dao.TThings.DeleteMap(gin.H{"uid": req.StationId})
	resp.Success(gin.H{})
	return
}
